from django.shortcuts import render
from django.views import View

# Create your views here.
from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories


class IndexView(View):
    def get(self,request):
        categories = get_categories()

        # 广告数据
        contents = {}
        content_categories = ContentCategory.objects.all()
        for cat in content_categories:
            contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

        # 渲染模板的上下文
        context = {
            'categories': categories,
            'contents': contents,
        }
        return render(request, 'index.html', context)
        # return render(request,"index.html")


# #############上传图片的代码################
# 以下代码在 python managepy shell中运行
# 1.导入Fdfs库
# from fdfs_client.client import Fdfs_client
# # 2.创建Fdfs客户端实例,让客户端实例加载配置文件
# #     因为配置文件可以找到tracker server
# client = Fdfs_client("utils/fastdfs/client.conf")
# # 3.上传文件
# client.upload_by_filename("/home/python/Desktop/1.jpg")
# 4.获取(分析)上传成功之后返回的数据

'''
getting connection
<fdfs_client.connection.Connection object at 0x7f3cb2bc1518>
<fdfs_client.fdfs_protol.Tracker_header object at 0x7f3cb2bc14e0>
{'Local file name': '/home/python/Desktop/1.jpg',
 'Status': 'Upload successed.',
  'Remote file_id': 'group1/M00/00/00/wKi-gF2vAM-AefXrADN0Q2ojgXk033.jpg',
   'Storage IP': '192.168.190.128',
   'Group name': 'group1',
   'Uploaded size': '3.00MB'}


'''